package MyAdmin.Config;

import MyAdmin.Filter.JwtAuthenticationTokenFilter;
import MyFramePro.Handler.AccessDeniedHandlerImpl;
import MyFramePro.Handler.AuthenticationEntryPointImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * 这里是后台 Security 的配置类
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)   //这里开启授权的相关配置
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;  //认证过滤器

    @Resource
    private AuthenticationEntryPointImpl authenticationEntryPoint;  //认证失败配置类

    @Resource
    private AccessDeniedHandlerImpl accessDeniedHandler;   //授权失败配置类

    /**
     *  定义了认证Authentication的方法
     */
    @Bean   //这里记得需要加上 Bean 注解
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {

        return super.authenticationManagerBean();
    }

    /**
     * 这里可以将用户密码进行加密的方法
     */
    @Bean
    public PasswordEncoder passwordEncoder(){

        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //1.关闭csrf
                .csrf().disable()

                //2.不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()

                //3.对于登录接口 允许匿名访问
                .antMatchers("/user/login").anonymous()

                /**
                 * 下面的需要 token 认证后才能进行访问的
                 */
                .antMatchers("/content/tag/list").authenticated()
                .antMatchers("/getInfo").authenticated()
                .antMatchers("/getRouters").authenticated()
                .antMatchers("/user/logout").authenticated()
                .antMatchers("/content/tag").authenticated()
                .antMatchers("/content/tag/{id}").authenticated()
                .antMatchers("/content/category/listAllCategory").authenticated()   //查询分类接口————写博文
                .antMatchers("/content/tag/listAllTag").authenticated() //查询所有标签接口————写博文
                .antMatchers("/content/article").authenticated()    //新增博文
                .antMatchers("/content/category/export").authenticated()    //导出分类到 Excel 中
                .antMatchers("/system/menu").authenticated()    //菜单
                .antMatchers("/system/role").authenticated()    //角色
                .antMatchers("/system/user").authenticated()    //用户

                //4.除上面外的所有请求全部不需要认证即可访问
                .anyRequest().permitAll();

        http.logout().disable();    //将 Security 中默认的注销接口关闭，不然自定义的退出接口无法访问到

        /**
         * 将认证过滤器放在用户名和密码过滤器的前面
         */
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        /**
         * 这里进行配置异常的处理器 (授权/认证)
         */
        http.exceptionHandling()
                .accessDeniedHandler(accessDeniedHandler)   //授权失败配置类
                .authenticationEntryPoint(authenticationEntryPoint);    //认证失败配置类

        //允许跨域
        http.cors();
    }

}
